import numpy as np
import matplotlib.pyplot as plt

# 定义数据点
# x = np.array([153.3, 164.9, 168.1, 151.5, 157.8, 156.7, 161.1])
# y = np.array([45.5, 56.0, 55.0, 52.8, 55.6, 50.8, 56.4])

x = np.array([7, 5, 9])
y = np.array([8, 4, 8])

# 计算x和y的平均值
x_mean = np.mean(x)
y_mean = np.mean(y)

# 计算分子和分母的和
numerator = np.sum((x - x_mean)*(y - y_mean))
denominator = np.sum((x - x_mean)**2)

# 计算斜率p
p = numerator / denominator

# 计算截距q
q = y_mean - p * x_mean

# 创建一系列x值用于绘制回归线
xs = np.linspace(min(x), max(x), 100)
# 使用回归方程计算对应的y值
ys = p * xs + q

# 返回p和q的值
print(p, q)

# 绘制散点图
plt.scatter(x, y, label='Data Points')

# 绘制线性回归线
plt.plot(xs, ys, 'r', label='Linear Regression Line')

# plt.plot(0, 0, 'go')
# plt.plot(-1, -1, 'ko')

# 添加标题和图例
plt.title('Scatter Plot with Your Linear Regression Equation')
plt.xlabel('X Axis Label')
plt.ylabel('Y Axis Label')
plt.legend()

# 显示图形
plt.show()